/**
 *Main Class 
 *
 * version 1.0
 *
 * @author Jin Kit Chua
 * @version 20130319 V1.0
 */

package ch.zbw.iCoach.kit;

//specific imports for this class
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.zbw.iCoach.remo.MainLogic;
import ch.zbw.iCoach.remo.UserList;
import ch.zbw.iCoach.remo.User;

//initialize the class
public class WelcomeScreen {

	// Main frame 
	private JFrame mainFrame; // tha mainframe

	// Buttons for the welcome screen
	private JButton createUser;// button to add an user
	private JButton deleteUser;// button to delete a user
	private JButton start;// button to start the icoach
	private JButton admin;// button to open the settingsscreen
	private JButton help;// help button to call the helpfile
	private JButton close;// close button to close the application

	// Datafields for classes
	private UserList ul;// class to get the user
	private MainLogic ml;// class to communicate the methodes
	private WelcomeScreen ws;// this.class to forward it self
	private Languages la;// class to genereate all the systemlanguages

	// datafield for the dropdown
	private JComboBox dropdown;// dropdown to select all the users

	// datafield for the pannels
	private JPanel buttonPanel;// panel to assamble all the used buttons
	private JPanel userPanel;// panel to assamble the users
	private JPanel rightPanel;// for formating a correct layout of the
								// welcomescreen

	// datafield for the labels
	private JLabel firstn;// label to show the selected users firstname
	private JLabel lastn;// label to show the selected users lastname

	// datafield for the integers
	private int index;// integer for the selected user in the array
	private int langIndex = 0;// integer to save the selected index for
								// systemlanguage

	// datafields for String objects
	private String lstn = "";// String to show the selected users firstname
	private String fstn = "";// String to show the selected users lastname
	private String selectedUser = null;

	/**
	 * 
	 * @param ul reference of the UserList
	 * @param ml reference of the Mainlogic so it can use the methods
	 */
	public WelcomeScreen(UserList ul, MainLogic ml) {

		this.ws = this;// transfer it self
		this.ul = ul;// transfer the userlist from mainlogic
		this.ml = ml;// transfer the refference from the mainlogic
		this.la = new Languages(this.langIndex);// initialize the languageclass
												// and transfering it

		this.mainFrame = new JFrame();// initialize the mainframe

		// initializing all the required buttons
		this.createUser = new JButton();
		this.deleteUser = new JButton();
		this.start = new JButton();
		this.admin = new JButton();
		this.close = new JButton();
		this.help = new JButton();

		// initializing the dropdown
		this.dropdown = new JComboBox();

		// initializing the panels
		this.buttonPanel = new JPanel();
		this.userPanel = new JPanel();
		this.rightPanel = new JPanel();

		// initializing the labels
		this.firstn = new JLabel();
		this.lastn = new JLabel();

		// filling the dropdown with exiting users from userlist
		int size = this.ul.getSize();
		int c = 0;
		this.dropdown.removeAllItems();
		while (c < size) {
			User u = this.ul.getUser(c);
			this.dropdown.addItem(u.getUsername());
			c++;
		}
		this.paint();// starting the gui for the welcomescreen
	}

	public void paint() {

		// Initialise frame and GUI elements
		this.mainFrame.setBackground(Color.lightGray);
		this.mainFrame.setResizable(false);
		// Exit Application while pressing close-Box
		this.mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.updateLang(); // calling the method to create the text for the
							// selected language

		// Set layout of all panels and frame
		this.rightPanel.setLayout(new BorderLayout(10, 20));
		this.userPanel.setLayout(new GridLayout(3, 1, 10, 30));
		this.mainFrame.setLayout(new BorderLayout(10, 20));
		this.buttonPanel.setLayout(new GridLayout(6, 1, 20, 20));

		// Add buttons
		this.buttonPanel.add(this.createUser);
		this.buttonPanel.add(this.deleteUser);
		this.buttonPanel.add(this.start);
		this.buttonPanel.add(this.admin);
		this.buttonPanel.add(this.close);
		// buttonPanel.add(this.help); // for further updates

		this.userPanel.add(this.dropdown);
		this.userPanel.add(this.firstn);
		this.userPanel.add(this.lastn);

		this.rightPanel.add(this.userPanel, BorderLayout.CENTER);
		this.mainFrame.add(this.buttonPanel, BorderLayout.WEST);
		this.mainFrame.add(this.rightPanel, BorderLayout.CENTER);
		// this.mainFrame.add(this.help, BorderLayout.SOUTH);
		this.mainFrame.setSize(800, 500);// setting standart sizes for the
											// mainscreens
		this.mainFrame.setVisible(true);
		this.mainFrame.setLocation(450, 250);// positioning the mainframe in the
												// center of the screen

		// initializing the required listeners for all the buttons
		this.createUser.addActionListener(new ActionListenerCreateUser());
		this.deleteUser.addActionListener(new ActionListenerDeleteUser());
		this.admin.addActionListener(new ActionListenerAdmin());
		this.start.addActionListener(new ActionListenerStart());
		this.close.addActionListener(new ActionListenerClose());

		// initializing the listener for the dropdown
		this.dropdown.addItemListener(new ActionListenerSelectedUser());
	}

	// method for closing the mainframe
	public void close() {
		ml.saveGame();
		mainFrame.dispose();
	}

	class ActionListenerClose implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			new CloseScreenMain(ws, la);// opening the closescreenmain with
										// reference of itself and languages
		}
	}

	class ActionListenerCreateUser implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			new CreateUserScreen(ml, la);// opening the adduserscreen with
											// reference of mainlogic and
											// languages
		}
	}

	class ActionListenerAdmin implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			new AdminScreen(ml, ws, la, langIndex, selectedUser);// opening the
																	// settingscreen
																	// with
																	// refereces
																	// of
																	// mainlogig,itself,index
																	// of
																	// selcted
																	// languag,
																	// and the
																	// selected
																	// user
		}
	}

	class ActionListenerDeleteUser implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			new DeleteScreen(ws, fstn, lstn, la);// opening the screen to delete
													// the users
		}
	}

	// method to delete the user
	public void DeleteUser() {
		int a = this.dropdown.getSelectedIndex();
		if (a < 0) {
			a = -a;
		}
		this.ml.deleteUser(a);// calling the method of the mainlogig to delete
								// the selected user
		this.ml.update();// calling the method to update the dropdown
	}

	// starting the actual icoach
	class ActionListenerStart implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int size = ul.getSize();
			if (size < 1) {
				start.setEnabled(false);// disable the button when no user is
										// selected
			} else {
				ml.startGame(index, la);// start the game over the mainlogic
			}
		}
	}

	// method to update the dowpdown
	public void update() {
		boolean noUser = false;
		int size = this.ul.getSize();// get the actual size of existing users
										// from userlist
		int c = 0;
		try {
			this.dropdown.removeAllItems();// first to remove all user from the
											// dropbox
		} catch (Exception e1) {
			ml.printMessage(e1.getMessage());
			noUser = true;
		}

		if (noUser == false) {// if a user excist
			this.admin.setEnabled(true);// enable the button
			this.start.setEnabled(true);// enable the button
			while (c < size) {
				User a;
				a = null;
				try {
					a = this.ul.getUser(c);
				} catch (IndexOutOfBoundsException e) {
					ml.printMessage(e.getMessage());
				}
				this.dropdown.addItem(a.getUsername());// adding usernames to
														// the empty dropdown
				c++;
			}
		}
	}

	// get the selcted user to an index integer
	class ActionListenerSelectedUser implements ItemListener {
		public void itemStateChanged(ItemEvent e) {
			int a = 0;
			try {
				a = dropdown.getSelectedIndex();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			if (a < 0) {
				a = -a;
				a -= 1;
			}
			lstn = ml.getLastName(a);// getting the lastname from mainlogic to
										// disply it on screen
			fstn = ml.getFirstName(a);// getting the firstname from mainlogic to
										// disply it on screen
			selectedUser = ml.getUserName(a);
			lastn.setText("     Lastname:  " + lstn);
			firstn.setText("     Firstname: " + fstn);
			index = a;
		}
	}

	// if systemlanguages is beeing changed, the index will change and create a
	// new languages with selcted index
	public void setLangIndex(int index) {
		this.langIndex = index;
		this.la = new Languages(this.langIndex);
		this.updateLang();// updates the languages
		new AdminScreen(this.ml, this.ws, this.la, this.langIndex,
				this.selectedUser);// restarting the adminscreen with selected
									// language
	}

	// updating the language with the selected language
	public void updateLang() {
		String tempLang = this.la.switchLangWelcome();// calling the method for
														// this screen language
														// change
		String[] parts = tempLang.split(",");// splitting the string with ,
		String buttonCreate = parts[0];// saving the language strings in a temp
										// array
		String buttonDel = parts[1];
		String buttonStart = parts[2];
		String buttonAdmin = parts[3];
		String buttonClose = parts[4];
		String buttonHelp = parts[5];
		String labelFst = parts[6];
		String labelLst = parts[7];
		String title = parts[8];
		// transfering the language words to the screen
		this.createUser.setText(buttonCreate);
		this.deleteUser.setText(buttonDel);
		this.start.setText(buttonStart);
		this.admin.setText(buttonAdmin);
		this.close.setText(buttonClose);
		this.help.setText(buttonHelp);
		this.firstn.setText(labelFst);
		this.lastn.setText(labelLst);
		this.mainFrame.setTitle(title);
	}
}
